/***************************************************************************** 
** XXX img_check.c XXX
** 
** Copyright (c) 2009~2014 ShangHai Infotm Ltd all rights reserved. 
** 
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** Description: check u-boot.img validity in RAM.
**
** Author:
**     Warits   <warits.wang@infotm.com>
**      
** Revision History: 
** ----------------- 
** 1.1  XXX 03/17/2010 XXX	Warits
*****************************************************************************/

#include <common.h>
/*
 * This function check the image validity in RAM,
 * and return the pointer to real image if check passed.
 * return NULL if no available image is found.
 */
void * check_ubimg(void)
{
	image_header_t *header = (image_header_t *)CONFIG_SYS_NAND_U_BOOT_START;
	uint8_t *data = (uint8_t *)(CONFIG_SYS_NAND_U_BOOT_START + 0x40);

	/* First: Check Magic Number */
	if(be32_to_cpu(header->ih_magic) != IH_MAGIC)
	  return NULL;

	/* Do not check CRC if size is too big, it may be a wrong header !! */
	if(be32_to_cpu(header->ih_size) > CONFIG_SYS_NAND_U_BOOT_SIZE)
	  return NULL;

	/* Second: Check CRC32 */
	if(be32_to_cpu(header->ih_dcrc) != crc32(0, data, be32_to_cpu(header->ih_size)))
	  return NULL;

	return (void *)(be32_to_cpu(header->ih_load));
}
